package com.leantech.android.browser.dialog

import android.Manifest
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.leantech.android.browser.R
import com.leantech.android.browser.ui.browser.adapter.TabPreviewAdapter
import com.leantech.android.browser.utils.SaveDrawableImageUtils
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.toast
import com.permissionx.guolindev.PermissionX

/**
 * date：2023/12/14 11:07
 * desc：
 */
object DialogHelper {


    /**
     * 联系客服dialog
     */
    fun showServiceDialog(activity: FragmentActivity) {
        val dialog = CustomDialog(activity)
        val view = LayoutInflater.from(activity).inflate(R.layout.dialog_service, null)

        val must_save_any = view.findViewById<View>(R.id.must_save_any)
        val must_close_any = view.findViewById<View>(R.id.must_close_any)

        must_save_any.onThrottleClick {
            val topPermissionTipsDialog = getTopPermissionTipsDialog(activity)
            if (!activity.isFinishing) {
                topPermissionTipsDialog.show()
                PermissionX.init(activity)
                    .permissions(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    )
                    .request { allGranted, grantedList, deniedList ->
                        topPermissionTipsDialog.dismiss()
                        if (allGranted) {
                            SaveDrawableImageUtils.saveDrawableImageToGallery(
                                activity,
                                R.drawable.img_dialog_service_qrcode
                            )
                        } else {
                            activity.toast("请授予存储权限")
                        }
                    }
            }
        }

        must_close_any?.onThrottleClick {
            dialog.dismiss()
        }

        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setContentView(view)

        if (!activity.isFinishing) {
            dialog.show()
        }
    }

    /**
     * 权限申请顶部说明dialog
     */
    fun getTopPermissionTipsDialog(
        activity: Activity,
        permissionName: String = "存储权限",
        permissionDesc: String = "为联系客服提供保存二维码到相册功能"
    ): CustomDialog {
        val dialog = CustomDialog(activity)
        val view = LayoutInflater.from(activity).inflate(R.layout.dialog_top_permission_tips, null)

        val must_permission_name_tv = view.findViewById<TextView>(R.id.must_permission_name_tv)
        val must_permission_desc_tv = view.findViewById<TextView>(R.id.must_permission_desc_tv)

        must_permission_name_tv.text = permissionName
        must_permission_desc_tv.text = permissionDesc

        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setContentView(view)

        return dialog
    }

    /**
     * Vip dialog
     */
    fun showVipDialog(
        activity: Activity,
        dialogLayoutRes: Int,
        onConfirm: (onTaskCompleted: () -> Unit) -> Unit,
        onCancel: () -> Unit = {}
    ) {
        val dialog = CustomDialog(activity)
        val view = LayoutInflater.from(activity).inflate(dialogLayoutRes, null)

        val must_confirm_any = view.findViewById<View>(R.id.must_confirm_any)
        val must_cancel_any = view.findViewById<View>(R.id.must_cancel_any)

        must_confirm_any?.onThrottleClick {
            onConfirm {
                dialog.dismiss()
            }
        }

        must_cancel_any?.onThrottleClick {
            dialog.dismiss()
            onCancel()
        }

        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setContentView(view)

        if (!activity.isFinishing) {
            dialog.show()
        }
    }

    /**
     * 切换窗口样式dialog
     */
    fun showChangeItemStyleDialog(
        activity: Activity,
        curItemStyle: Int,
        onConfirm: (itemStyle: Int) -> Unit,
        onCancel: () -> Unit = {}
    ) {
        val dialog = CustomDialog(activity)
        val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_item_style, null)

        val option_1 = view.findViewById<View>(R.id.option_1)
        val option_2 = view.findViewById<View>(R.id.option_2)
        val option_3 = view.findViewById<View>(R.id.option_3)
        val must_cancel_any = view.findViewById<View>(R.id.must_cancel_any)

        when (curItemStyle) {
            TabPreviewAdapter.ITEM_STYLE_CAROUSEL -> {
                option_1.isSelected = true
                option_2.isSelected = false
                option_3.isSelected = false
            }

            TabPreviewAdapter.ITEM_STYLE_LINEAR -> {
                option_1.isSelected = false
                option_2.isSelected = false
                option_3.isSelected = true
            }

            else -> {
                option_1.isSelected = false
                option_2.isSelected = true
                option_3.isSelected = false
            }
        }

        option_1?.onThrottleClick {
            dialog.dismiss()
            onConfirm(TabPreviewAdapter.ITEM_STYLE_CAROUSEL)
        }

        option_2?.onThrottleClick {
            dialog.dismiss()
            onConfirm(TabPreviewAdapter.ITEM_STYLE_GRID)
        }

        option_3?.onThrottleClick {
            dialog.dismiss()
            onConfirm(TabPreviewAdapter.ITEM_STYLE_LINEAR)
        }

        must_cancel_any?.onThrottleClick {
            dialog.dismiss()
            onCancel()
        }

        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setContentView(view)

        if (!activity.isFinishing) {
            dialog.show()
        }
    }

    class ImageBlockBean(val id: Int, val title: String)
}